package org.dentalcard.ui.schedule;

import org.dentalcard.entity.Shift;
import org.dentalcard.service.PersistenceManager;
import org.dentalcard.ui.api.tools.DecDialog;
import org.dentalcard.ui.i18n.I18nManager;
import org.dentalcard.ui.main.MainFrameBuilder;
import org.dentalcard.ui.schedule.tools.ShiftTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @user: Vladimir Chernishov
 * @date: 25.02.12
 * @time: 16:31
 */
public class ShiftsPopup extends DecDialog {

    private final JPanel main;
    private JTable shift;
    private ShiftTableModel shiftModel;

    public ShiftsPopup(Frame owner) {
        super(owner, I18nManager.getRes("shifts.screen.name"), true);
        main = new JPanel(new GridBagLayout());
        this.getContentPane().add(new JScrollPane(main));
        setPreferredSize(new Dimension(400, 400));
        setResizable(true);
        init();
        pack();
        center(owner);
    }

    private void init() {
        shiftModel = new ShiftTableModel();
        shift = new JTable(shiftModel);

        createMenu();

        createShiftPopupMenu();

        int rowN = 0;
        main.add(shift.getTableHeader(), new GridBagConstraints(0, rowN++, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
        main.add(shift, new GridBagConstraints(0, rowN++, 1 ,1, 1, 0, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
        main.add(new JPanel(), new GridBagConstraints(0, rowN++, 1 ,1, 1, 1, GridBagConstraints.FIRST_LINE_START,
                GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

        JButton ok = new JButton("OK");
        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                ShiftsPopup.this.dispose();
            }
        });
        main.add(ok, new GridBagConstraints(0, rowN++, 2 ,1, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.HORIZONTAL, new Insets(0, 5, 0, 5), 0, 0));
    }

    private void createMenu() {
        JMenuBar menu = new JMenuBar();
        JButton newShift = new JButton(I18nManager.getRes("button.shifts.new"));
        newShift.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                newShift();
            }
        });
        menu.add(newShift);

        JButton deleteShift = new JButton(I18nManager.getRes("button.shifts.delete"));
        deleteShift.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteShift();
            }
        });
        menu.add(deleteShift);

        this.getRootPane().setJMenuBar(menu);
    }

    private void createShiftPopupMenu() {
        final JPopupMenu shitTableMenu = new JPopupMenu("menu");
        JMenuItem newMenu = new JMenuItem("New");
        newMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                newShift();
            }
        });
        shitTableMenu.add(newMenu);
        JMenuItem delete = new JMenuItem("Delete");
        delete.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteShift();
            }
        });
        shitTableMenu.add(delete);

        shift.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton() == MouseEvent.BUTTON3){
                    shitTableMenu.show((Component) e.getSource(), e.getX(), e.getY());
                }
            }
        });
    }

    private void newShift() {
        new AddNewShiftPopup(MainFrameBuilder.getMainFrame()).setVisible(true);
        shiftModel.reload();
    }

    private void deleteShift() {

        int selectedRow = shift.getSelectedRow();
        if(selectedRow < 0){
            JOptionPane.showMessageDialog(this, I18nManager.getRes("please.select.a.row.to.delete"));
            return;
        }
        Shift s = shiftModel.getShift(selectedRow);
        if(s == null){
            return;
        }
        if (JOptionPane.showConfirmDialog(this, I18nManager.getRes("are.you.sure.to.delete.shift", s.getName())) == JOptionPane.YES_OPTION) {
            PersistenceManager.delete(s);
            shiftModel.reload();
        }
    }
}
